--团队副本通用函数
--#include "data\config\fuben\TeamFubenConfig.lua" once

TEAMFUBEN_TEAM_ID		= 1 			--团队ID

--[[
注册一些事件
]]
function ResisterTeamFubenProc()
	--print("ResisterTeamFubenProc...")
	for fubenIdx, fubenCfg in ipairs( TeamFubenConfig ) do
		SetNeedReloginFuben( fubenCfg.fubenId, GlobalLuaConfig.reloginFuben.teamFuben, GetElemsForReLoginTeamFuben )		
					--该副本是需要掉线恢复的
		Fuben.addTeamFubenId( fubenCfg.fubenId )											--告诉引擎此副本是团队副本

		SceneExitDispatcher.register( fubenCfg.sceneId, OnExitTeamFuben )					--注册离开场景、副本后触发的事件
		SceneHandExitDispatcher.register( fubenCfg.sceneId, OnHandExitTeamFuben )			--注册手工离开场景、副本后触发的事件
		FubenTimeoutDispatcher.register( fubenCfg.fubenId, OnTeamFubenTimeout )				--注册场景、副本时间到后触发的事件
		SceneLogoutDispatcher.register( fubenCfg.sceneId, OnCommonFubenLogout )				--注册场景、副本中下线触发的事件
	end
end

--[[
为副本掉线恢复，获取一些数据
]]
function GetElemsForReLoginTeamFuben( fubenPtr )
	local fubenId 	= Fuben.getFuBenId(fubenPtr)    
	local fubenCfg 	= GetTeamFubenConfig(fubenId)
	local InitFubenRightPanel = InitTeamFubenPanel
	local loginLog   	= "team fuben login" 
	local loginLogOk	= "team fuben  login ok"
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end


--[[
玩家下线，可能在团队副本外，也可能在团队副本中
]]
function OnLogoutTeamFubenAll( sysarg )
	--local teamId  = Fuben.getFubenTeamId( sysarg )
	local fubenId = Actor.getFubenId( sysarg )
	if not IsInTeamFuben( fubenId ) then
		local actorId 	= Actor.getActorId(sysarg)
		local teamId   	= Fuben.searchFubenTeamId( actorId )
		--print("OnLogoutTeamFubenAll,  not from team fuben， teamId="..teamId)
		if teamId <= 0 then
			return
		end
		DoQuitFubenTeam( sysarg ) 				--副本外下线，要退出队伍
		BroadFubenTeamInfo( teamId ) 			--向队友发送最新成员信息
	end
end

--[[
已经退出团队副本场景，不需要退出队伍，要支持掉线重登
注意：手工退出、下线、被T出，GM指令Move出都会触发
]]
function OnExitTeamFuben( sysarg, sceneId, fubenId )
	--print("OnExitTeamFuben, sceneId="..sceneId..", fubenId="..fubenId)
	local fubenCfg = GetTeamFubenConfig(fubenId)
	if not fubenCfg then
	    return
	end
	Fuben.setTeamFubenCanAward( sysarg, false )		--缺少通关无奖励，要求玩家必须通过合服途径进入团队副本才有奖励
	ClearTeamFubenKillBuff( sysarg )				--清除副本buff
	CloseActivityRightPanel(sysarg)
end

--[[
手工离开场景
1、手工离开要退出队伍
]]
function OnHandExitTeamFuben( sysarg )
	--print("OnHandExitTeamFuben...")
	DoQuitFubenTeam( sysarg ) 					--手工离开场景，算退出团队
end

--[[
副本时间到,算通关失败，并解散团队
]]
function OnTeamFubenTimeout( fubenPtr )
	local fubenId = Fuben.getFuBenId(fubenPtr)
	--print("OnTeamFubenTimeout, fubenId="..fubenId)
	local fubenCfg = GetTeamFubenConfig(fubenId)
	if not fubenCfg then
	    return
	end

	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		OnTeamFubenOver( player, fubenCfg, 0, 0 )
	end

	local teamId  = Fuben.getFbValue( fubenPtr, TEAMFUBEN_TEAM_ID )
	--print("OnTeamFubenTimeout, teamId="..teamId)
	Fuben.dismissFubenTeam( teamId )						--解散副本团队
end

function IsInTeamFuben( fubenId )
	for fubenIdx, fubenCfg in ipairs( TeamFubenConfig ) do
		if fubenCfg.fubenId == fubenId then
			return true
		end
	end
	return false
end


--[[
发送本人的团队副本
]]
function SendTeamFubenData( sysarg )
	local sVar = Actor.getStaticVar( sysarg )
	if not sVar.teamFBEnterTimes then
		sVar.teamFBEnterTimes = {}
	end
	local pack = DataPack.allocPacket(sysarg, enFubenSystemID, sSendTeamFubenData)
	if pack then
		--local teamId = Fuben.getFubenTeamId( sysarg )
		local actorId 	= Actor.getActorId(sysarg)
		local teamId = Fuben.searchFubenTeamId( actorId )
		local fubenId= Fuben.getTeamFubenId( teamId )
		DataPack.writeInt(pack, fubenId)
		DataPack.writeInt(pack, teamId)
		--print("SendTeamFubenData, fubenId="..fubenId..", teamId="..teamId)
		DataPack.writeByte(pack, #TeamFubenConfig)
		for fubenIdx, fubenCfg in ipairs( TeamFubenConfig ) do
			DataPack.writeInt(pack, fubenCfg.fubenId)

			local maxTimes   = GetActorDailyMaxTimes( sysarg, enDailyOpTimes_TeamFuben, fubenCfg.fubenId )
			local enterTimes = GetActorDailyDoneTimes( sysarg, enDailyOpTimes_TeamFuben, fubenCfg.fubenId )
			local lastTimes  = maxTimes - enterTimes
			if lastTimes < 0 then
				lastTimes = 0
			end
			DataPack.writeWord(pack, maxTimes)
			DataPack.writeWord(pack, lastTimes)
			--print("SendTeamFubenData, maxTimes="..maxTimes..", lastTimes="..lastTimes)
		end

		DataPack.flush(pack)
	end
end

--[[
通知玩家，他加入了队伍
]]
function NotifyJoinTeamFuben( sysarg )
	--print("NotifyJoinTeamFuben...")
	local pack = DataPack.allocPacket(sysarg, enFubenSystemID, sNotifyJoinFubenTeam)
	if pack then
		--local teamId = Fuben.getFubenTeamId( sysarg )
		local actorId = Actor.getActorId(sysarg)
		local teamId  = Fuben.searchFubenTeamId( actorId )
		local fubenId = Fuben.getTeamFubenId( teamId )
		DataPack.writeInt(pack, fubenId)
		DataPack.writeInt(pack, teamId)
		DataPack.flush(pack)
	end
end

--[[
通知玩家，他退出了队伍
]]
function NotifyQuitTeamFuben( sysarg )
	--print("NotifyQuitTeamFuben...")
	local pack = DataPack.allocPacket(sysarg, enFubenSystemID, sNotifyQuitFubenTeam)
	if pack then
		DataPack.flush(pack)
	end
end

--[[
通知玩家，他被踢出了队伍
]]
function NotifyKickedTeamFuben( sysarg )
	--print("NotifyKickedTeamFuben...")
	local pack = DataPack.allocPacket(sysarg, enFubenSystemID, sNotifyKickedFubenTeam)
	if pack then
		DataPack.flush(pack)
	end
end

--[[
下发全部副本队伍信息
]]
function SendFubenAllTeams( sysarg, fubenId )
	--print("SendFubenAllTeams, fubenId="..fubenId)
	Fuben.sendFubenAllTeams( sysarg, fubenId )
end

--[[
获取本人团队信息
]]
function SendFubenMyTeam( sysarg )
	Fuben.sendFubenMyTeam( sysarg )
end


--创建团队副本
function CreateFubenTeam( sysarg, fubenId )
	--print("CreateFubenTeam, fubenId="..fubenId)
    local cfg = GetTeamFubenConfig(fubenId)
	if not cfg then
	    return
	end

	if not Actor.checkActorLevel( sysarg, cfg.levelLimit[2], cfg.levelLimit[1] ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben027, ttFlyTip)
		return
	end

	--判断是否可以进入场景
	if not CanEnterTeamFuben( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben022, ttFlyTip)
		return
	end

	--冷却时间
	local dVar 		= Actor.getDyanmicVar(sysarg)
	local createCD 	= dVar.createTeamFubenCD or 0
	local now		= System.getCurrMiniTime()
	if createCD > 0 and now < createCD then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.TeamFuben023),ttFlyTip)		
		return
	end
	
	--是否有之前的普通队伍
	local hasTeam = false
	--如果在普通队伍中，退出普通队伍
	if Actor.isInTeam(sysarg) then
	    hasTeam = true
	    Actor.exitTeam(sysarg)
	end

	--是否有之前的团队副本
	if Actor.hasState(sysarg,esStateTeamFuben) then
	    hasTeam = true
	    DoQuitFubenTeam( sysarg )
	end

	--创建
	local teamId = Fuben.createTeamFuben( sysarg, cfg.fubenId )
	if teamId <= 0 then
		return
	end
	local actorId = Actor.getActorId(sysarg)
	local myTeamId = Fuben.searchFubenTeamId( actorId )
	--print("CreateFubenTeam, myTeamId="..myTeamId)

	local fubenCfg = FuBen[cfg.fubenId]
	local msg = string.format( Lang.ScriptTips.TeamFuben008, Actor.getName(sysarg), fubenCfg.fbname, cfg.fubenId, teamId )
	if hasTeam then
	    Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben024,ttFlyTip) 
    else
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben021,ttFlyTip) 
    end
    OnMemberJoinFubenTeam( sysarg, teamId )
	System.broadcastTipmsg( msg, ttChatWindow)
	dVar.createTeamFubenCD  = now + cfg.createCD
end

--[[
快速加入副本团队,如果没有队伍则创建团队
]]
function QuickJoinFubenTeam( sysarg, fubenId )
	--print("QuickJoinFubenTeam, fubenId="..fubenId)
    local cfg = GetTeamFubenConfig(fubenId)
	if not cfg then
	    return
	end
	local teamId = Fuben.getTeamIdOfMemberMax( fubenId )			--找到一个人数最多的副本
	if teamId > 0 then
		JoinFubenTeam( sysarg, teamId )
	else
		CreateFubenTeam( sysarg, fubenId )
	end
end

--[[加入团队副本
团队不存在、已经在副本中的情况下，不能加入
]]
function JoinFubenTeam( sysarg, teamId )
	local fubenId  = Fuben.getTeamFubenId( teamId )
	--print("JoinFubenTeam, teamId="..teamId..", fubenId="..fubenId)
    local fubenCfg = GetTeamFubenConfig(fubenId)
	if not fubenCfg then
	    return
	end

	if not Actor.checkActorLevel( sysarg, fubenCfg.levelLimit[2], fubenCfg.levelLimit[1] ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben028, ttFlyTip)
		return
	end

	--判断是否可以进入场景
	if not CanEnterTeamFuben( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben029, ttFlyTip)
		return
	end

	--local myTeamId = Fuben.getFubenTeamId( sysarg )				--本人已经有的队伍
	local actorId  = Actor.getActorId(sysarg)
	local myTeamId = Fuben.searchFubenTeamId( actorId )				--本人已经有的队伍
	--print("JoinFubenTeam, myTeamId="..myTeamId)
	if  myTeamId > 0 then
	    Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben002, ttFlyTip)
		return
	end

	if not Fuben.isFubenTeamExist( teamId ) then		--副本团队不存在
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben014, ttFlyTip)
		return
	end

	if Fuben.isFubenTeamInFuben( teamId ) then		--团队已经进入副本了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben015, ttFlyTip)
		return
	end

	local memberCount = Fuben.getTeamMemberCount( teamId )
	if memberCount >= fubenCfg.playerCountMax or memberCount >= 5  then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben026, ttFlyTip)
		return
	end

	--是否有之前的普通队伍
	local hasTeam = false
	--如果在普通队伍中，退出普通队伍
	if Actor.isInTeam(sysarg) then
	    hasTeam = true
	    Actor.exitTeam(sysarg)
	end
	--是否有之前的团队副本
	if Actor.hasState(sysarg,esStateTeamFuben) then
	    hasTeam = true
	    DoQuitFubenTeam( sysarg )
	end
	--加入
	if Fuben.joinFubenTeam(sysarg,teamId) then
	 	if hasTeam then
	 		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben025, ttFlyTip+ttChatWindow ) 
        end
        OnMemberJoinFubenTeam( sysarg, teamId )
    end
end

function OnMemberJoinFubenTeam( memberPtr, teamId )
	--print("OnMemberJoinFubenTeam, teamId="..teamId)
	NotifyJoinTeamFuben( memberPtr )
	SendTeamFubenData( memberPtr )
	BroadFubenTeamInfo( teamId ) 			--向队友发送最新成员信息
	if not CanEnterTeamFuben(memberPtr) then
		Actor.sendTipmsg(memberPtr, Lang.ScriptTips.TeamFuben013, ttFlyTip)
	end
end

--[[
向队伍成员广播队伍信息
]]
function BroadFubenTeamInfo( teamId )
	local memberCount = Fuben.getTeamMemberCount( teamId )
	for idx=1, memberCount do
	    local memberId = Fuben.getTeamMemberId( teamId, idx )
	    --print("BroadFubenTeamInfo, memberId="..memberId)
	    local memberPtr = System.getEntityPtrByActorID(memberId)
	    if memberPtr then
	    	SendFubenMyTeam( memberPtr )
	    end
	end
end

--[[
退出本人队伍
]]
function QuitFubenTeam( sysarg )
	--local teamId  = Fuben.getFubenTeamId( sysarg )
	local actorId = Actor.getActorId(sysarg)
	local teamId  = Fuben.searchFubenTeamId( actorId )
	--print("QuitFubenTeam, teamId="..teamId)
	if teamId <= 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben040,ttFlyTip)
		return
	end

	if Fuben.isFubenTeamInFuben( teamId ) then		--团队已经进入副本，不能退出
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben018, ttFlyTip)
		return
	end

	DoQuitFubenTeam( sysarg )
end

function DoQuitFubenTeam( sysarg )
	--print("DoQuitFubenTeam...")
	Fuben.quitFubenTeam( sysarg )
	NotifyQuitTeamFuben( sysarg )
	BroadFubenTeamInfo( teamId ) 			--向队友发送最新成员信息
end

--[[
将成员踢出队伍
]]
function KickFubenTeam( sysarg, memberId )
	--local teamId = Fuben.getFubenTeamId( sysarg )
	local actorId = Actor.getActorId(sysarg)
	local teamId  = Fuben.searchFubenTeamId( actorId )
	local captainId = Fuben.getTeamCaptainId( teamId )
	--print("KickFubenTeam, teamId="..teamId..", captainId="..captainId)
	if teamId <= 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben040,ttFlyTip)
		return
	end
	
	if Actor.getActorId( sysarg ) ~= captainId then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben041,ttFlyTip)
		return
	end

	if Actor.getActorId( sysarg ) == memberId then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben042,ttFlyTip)
		return
	end

	if Fuben.isFubenTeamInFuben( teamId ) then		--团队已经进入副本了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben017, ttFlyTip)
		return
	end

	Fuben.kickFubenTeam( sysarg, memberId ) 			--踢出成功，引擎调用LUA的OnKickedTeam()
end

--[[创建&&加入团队副本条件检测
]]
function CanEnterTeamFuben(sysarg)
	if Actor.canBeTelported( sysarg ) ~= enCanntBeTeledReason_No then
		return false
	end

	local sceneId = Actor.getSceneId( sysarg )
	if IsElemInList( CantTelToTeamFubenFromScenes, sceneId ) then
		return false
	end

	return true
end


--被T出团队副本
function  OnKickedTeam(sysarg, teamId, fubenId )
	--print("OnKickedTeam, teamId="..teamId..", fubenId="..fubenId)
	--[[
	local dVar = Actor.getDyanmicVar(sysarg)
	if not dVar.KickedTeam then
	    dVar.KickedTeam = {}
	end
    dVar.KickedTeam[teamId] = System.getCurrMiniTime()
	]]
	NotifyKickedTeamFuben( sysarg )
    BroadFubenTeamInfo( teamId ) 			--向队友发送最新成员信息
end

--[[
邀请加入副本，对方要在线
]]
function InviteFubenTeam( sysarg, objActorId )
	--print("InviteFubenTeam, objActorId="..objActorId)
	if objActorId <= 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0085,ttFlyTip)
		return
	end
	--local teamId = Fuben.getFubenTeamId( sysarg )
	local myActorId = Actor.getActorId(sysarg)
	local myTeamId  = Fuben.searchFubenTeamId( myActorId )
	--print("InviteFubenTeam, myTeamId="..myTeamId)
	if myTeamId <= 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben005,ttFlyTip)
		return
	end

	local fubenId = Fuben.getTeamFubenId( myTeamId )
	local fubenCfg = GetTeamFubenConfig(fubenId)
	if not fubenCfg then
	    return
	end

	local captainId = Fuben.getTeamCaptainId( myTeamId )
	if Actor.getActorId( sysarg ) ~= captainId then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben006,ttFlyTip)
		return
	end

	if not Fuben.isFubenTeamExist( myTeamId ) then		--副本团队不存在
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben014, ttFlyTip)
		return
	end

	if Fuben.isFubenTeamInFuben( myTeamId ) then		--团队已经进入副本了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben016, ttFlyTip)
		return
	end

	local playerPtr = System.getEntityPtrByActorID(objActorId)
	if not playerPtr then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0011,ttFlyTip)
		return
	end

	local objTeamId  = Fuben.searchFubenTeamId( objActorId )			--目标的团队
	--print("InviteFubenTeam, objTeamId="..objTeamId)
	if objTeamId > 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben007,ttFlyTip)
		return
	end

	local pack = DataPack.allocPacket(playerPtr, enFubenSystemID, sSendFubenTeamInviteAsk)
	if pack then
		DataPack.writeUInt(pack, Actor.getActorId( sysarg ))
		DataPack.writeString(pack, Actor.getName( sysarg ))
		DataPack.writeString(pack, fubenCfg.fubenName)
		DataPack.writeInt(pack, myTeamId)
		DataPack.flush(pack)
	end

	local msg = string.format(Lang.ScriptTips.TeamFuben020, Actor.getName(playerPtr))
	Actor.sendTipmsg( sysarg, msg, ttChatWindow)
end

--系统招募
function TeamRecruit(sysarg,args)
	
end

--队长开启团队副本
function  BeginTeamFuben( sysarg )
	--local teamId = Fuben.getFubenTeamId( sysarg )
	local actorId = Actor.getActorId(sysarg)
	local teamId = Fuben.searchFubenTeamId( actorId )
	if teamId <= 0 then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben009,ttFlyTip)
		return
	end

	local fubenId   	= Fuben.getTeamFubenId( teamId )
	local captainId 	= Fuben.getTeamCaptainId( teamId )
	local memberCount 	= Fuben.getTeamMemberCount( teamId )
	--print("BeginTeamFuben, teamId="..teamId..", fubenId="..fubenId..", captainId="..captainId..", memberCount="..memberCount)
    local cfg = GetTeamFubenConfig(fubenId)
	if not cfg then
	    return
	end

	local captainId = Fuben.getTeamCaptainId( teamId )
	if Actor.getActorId( sysarg ) ~= captainId then
		Actor.sendTipmsg(sysarg,Lang.ScriptTips.TeamFuben010,ttFlyTip)
		return
	end

	--判断是否可以进入场景
	if not CanEnterTeamFuben( sysarg ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben012, ttFlyTip)
		return
	end

	if not Fuben.isFubenTeamExist( teamId ) then		--副本团队不存在
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben014, ttFlyTip)
		return
	end

	if Fuben.isFubenTeamInFuben( teamId ) then		--团队已经进入副本了
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.TeamFuben015, ttFlyTip)
		return
	end

	--创建副本
	local fubenHandle = Fuben.createFuBen(fubenId)
    if fubenHandle > 0 then
    	local fubenPtr = Fuben.getFubenPtr( fubenHandle )
    	if not fubenPtr then
    		return
    	end
    	Fuben.setFbValue( fubenPtr, TEAMFUBEN_TEAM_ID, teamId )			--设置副本归属的团队ID
    	Fuben.setFubenTeamInFuben( teamId, true )						--设置队伍已经在副本中

		FreshCommonMonsters( cfg.monsters, fubenHandle, true )
	    for idx=1, memberCount do
	    	local memberId = Fuben.getTeamMemberId( teamId, idx )
	    	--print("BeginTeamFuben, memberId="..memberId)
	    	local memberPtr = System.getEntityPtrByActorID(memberId)
	    	if memberPtr then
	    		if CanEnterTeamFuben( memberPtr ) then			--玩家能够被拉传
	    			if CheckActorDailyDoneTimes( memberPtr, enDailyOpTimes_TeamFuben, cfg.fubenId, 1 ) then
	    				AddActorDailyDoneTimes( memberPtr, enDailyOpTimes_TeamFuben, cfg.fubenId, 1 )
	    				Fuben.setTeamFubenCanAward( memberPtr, true )		--通关有奖励
	    			else 			--次数达到限额，可以参加，但没有奖励
	    				Fuben.setTeamFubenCanAward( memberPtr, false )		--通关无奖励
	    			end

	    			--print("BeginTeamFuben, do enter memberId="..memberId)
	    			if Actor.enterFuBen( memberPtr, fubenHandle, cfg.sceneId, cfg.enterPos[1], cfg.enterPos[2] ) then
	    				OnMemberEnterTeamFuben( memberPtr, teamId, cfg )
	    			end
	    			--SendTeamFubenData( sysarg )
	    		end
	    	end
	    end
	end
end

--[[
成员已经进入了团队副本
]]
function OnMemberEnterTeamFuben( memberPtr, teamId, fubenCfg )
	--print("OnMemberEnterTeamFuben, teamId="..teamId)
	InitTeamFubenPanel( memberPtr, fubenCfg )
	Actor.triggerQuestEvent(memberPtr, 45, 7, 1) 						--任务事件-参加团队副本1次
	if not Fuben.getTeamFubenCanAward( memberPtr ) then
		Actor.sendTipmsg(memberPtr, Lang.ScriptTips.TeamFuben019, ttChatWindow)
	end
end

function InitTeamFubenPanel( sysarg, fubenCfg )
	local contStyle 	= 1
	local buttons 		= {1}
	local activityId 	= enRightPanelActivity_TeamFuben
	local subActivityId = fubenCfg.idx
	OpenCommonStylePanel( sysarg, 1, activityId, subActivityId, fubenCfg.fubenName, fubenCfg, contStyle, buttons  )
end

--团队副本进入副本操作
function OnEnterTeamFuben(sysarg, fubenHandle)
	--print("OnEnterTeamFuben, fubenHandle="..fubenHandle)
end

--[[
--sysarg:归属玩家
--lastHitKiller：最后一击玩家
注意：怪物死亡时，玩家可能不在线，要确保副本进度能够继续进行
]]
function OnTeamFubenMonsterDie( monster, sysarg, monId, lastHitKiller )
	local fubenId 		= Actor.getFubenId(monster)
	local fubenPtr 		= Actor.getFubenPrt(monster)
	local fubenHandle	= Fuben.getFubenHandle(fubenPtr)
	--print("OnTeamFubenMonsterDie, monId="..monId..", fubenId="..fubenId..", fubenHandle="..fubenHandle)
	local fubenCfg = GetTeamFubenConfig(fubenId)
	if not fubenCfg then
	    return
	end

	local activitySts	= nil		--由面板自己判断
	local contStyle  	= 1
	local killAll 	 	= false
	local allBoss, killBoss, allMon, killMon = GetCommonBossMonsterNum6( fubenHandle, fubenCfg.monsters )
	if killBoss + killMon >= allBoss + allMon then 		--击杀成功
		killAll = true
	end

	local playerList = LuaHelp.getFbActorList( fubenPtr )
	for k, player in ipairs( playerList or {} ) do
		ChangeCommonStylePanel( player, activitySts, contStyle, fubenCfg, {1}, {1} )
		if killAll then
			local passTime = Fuben.getFbAllTime( fubenPtr ) - Fuben.getFbTime( fubenPtr )   --通关时间 = 副本全部时间-副本剩余时间
			--print("OnTeamFubenMonsterDie, passTime="..passTime)
			OnTeamFubenOver( player, fubenCfg, 1, passTime )
		end
	end

	if killAll then
		local teamId  = Fuben.getFbValue( fubenPtr, TEAMFUBEN_TEAM_ID )
		--print("OnTeamFubenMonsterDie, teamId="..teamId)
		Fuben.dismissFubenTeam( teamId )						--解散副本团队
		--Fuben.setFubenTime( fubenHandle, 1 )					--打完关闭副本(不要这里，会触发timeout)
		Fuben.setFbValue( fubenPtr, TEAMFUBEN_TEAM_ID, 0 )		--怪物打完，副本队伍设置为0，以防误进入
	else
		local buffCfg = fubenCfg.killBuff[monId]
		if buffCfg then
			local buffId = fubenCfg.killBuff[monId].buffId or 0
			if buffId > 0 then
				AddTeamFubenKillBuff( fubenPtr, buffId )
			end
		end
	end
end

--[[
击杀怪物，全体加buff
]]
function AddTeamFubenKillBuff( fubenPtr, buffId )
	--print("AddTeamFubenKillBuff, buffId="..buffId)
	local playerList = LuaHelp.getFbActorList(fubenPtr) or {}
	for i, player in ipairs( playerList or {} ) do
  		if Actor.hasBuffById(player, buffId) then
  			Actor.delBuffById(player, buffId)
  		end
  		Actor.addBuffById(player, buffId)
  	end
end


function ClearTeamFubenKillBuff( sysarg )
	for fubenIdx, fubenCfg in ipairs( TeamFubenConfig ) do
		for k, buffCfg in pairs( fubenCfg.killBuff or {} ) do
			--print("ClearTeamFubenKillBuff, buffId="..buffCfg.buffId)
			if Actor.hasBuffById(sysarg, buffCfg.buffId) then
  				Actor.delBuffById(sysarg, buffCfg.buffId)
  			end
		end
	end
end

--[[游戏通关
sts:0-失败，1-成功
]]
function OnTeamFubenOver( memberPtr, fubenCfg, sts, passTime )
	--print("OnTeamFubenOver, sts="..sts)
	local panelInfo = {}
	panelInfo.subActivityId = fubenCfg.idx
	panelInfo.sts 			= sts 			--这里是通过面板的状态

	if sts == 1 then
		local star  = MakeCommonPassStar( fubenCfg.star, passTime )
		panelInfo.star 			= star
		--panelInfo.awards 		= fubenCfg.starAwards[star]

		local canAward = Fuben.getTeamFubenCanAward( memberPtr )
		--[[
		if canAward then
			print("OnTeamFubenOver, "..Actor.getName( memberPtr ).." can get pass awards")
		else
			print("OnTeamFubenOver, "..Actor.getName( memberPtr ).." can not get pass awards")
		end
		]]

		if canAward then
			local logId  = GameLog.clTeamFubenPassAward
			local logStr = Lang.ScriptTips.TeamFubenLog01
			local mail = {}
			mail.title 		= string.format( Lang.ScriptTips.TeamFubenMailTitle01, fubenCfg.fubenName )
			mail.content 	= string.format( Lang.ScriptTips.TeamFubenMailCont01, fubenCfg.fubenName, star )

			local num 		= 1
			local awardType = 2			--从奖励库抽取
			local giveType 	= 2 		--直接通过邮件
			local passAwards = GiveCommonPassAwardsByStar( memberPtr, fubenCfg.starAwards, star, num, awardType, giveType, mail, logId, logStr )
			panelInfo.awards = passAwards

			Actor.sendTipmsg( memberPtr, Lang.ScriptTips.TeamFuben003,ttChatWindow )
		else
			Actor.sendTipmsg( memberPtr, Lang.ScriptTips.TeamFuben004,ttChatWindow )
		end
	end
	PassActivityRightPanel( memberPtr, 1, enRightPanelActivity_TeamFuben, panelInfo )
	Actor.exitFuben( memberPtr )
end

--查找团队副本配置
function GetTeamFubenConfig( fubenId )
    for fubenIdx, fubenCfg in ipairs( TeamFubenConfig ) do
	    if fubenCfg.fubenId == fubenId then
	    	return fubenCfg
		end
	end
	return nil
end


ResisterTeamFubenProc()
